package com.dgut.usermodule.entity;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
public class PermListDTO {
	private Long id;
	private String identifier;
	private String url;
	private String urlName;
	private String parentTree;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getIdentifier() {
		return identifier;
	}
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUrlName() {
		return urlName;
	}
	public void setUrlName(String urlName) {
		this.urlName = urlName;
	}
	public String getParentTree() {
		return parentTree;
	}
	public void setParentTree(String parentTree) {
		this.parentTree = parentTree;
	}
	public static Specification<Permission> getSpecification(PermListDTO permListDTO){
		Specification<Permission> spec = new Specification<Permission>() {
			@Override
			public Predicate toPredicate(Root<Permission> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (permListDTO != null && !StringUtils.isEmpty(permListDTO.getId())) {
					Predicate p = cb.like(root.get("id").as(String.class), "%"+permListDTO.getId()+"%");
					list.add(p);
				}
				if (permListDTO != null && !StringUtils.isEmpty(permListDTO.getIdentifier())) {
					Predicate p = cb.like(root.get("identifier").as(String.class), "%"+permListDTO.getIdentifier()+"%");
					list.add(p);
				}
				if (permListDTO != null && !StringUtils.isEmpty(permListDTO.getUrl())) {
					Predicate p = cb.like(root.get("url").as(String.class), "%"+permListDTO.getUrl()+"%");
					list.add(p);
				}
				if (permListDTO != null && !StringUtils.isEmpty(permListDTO.getUrlName())) {
					Predicate p = cb.like(root.get("urlName").as(String.class), "%"+permListDTO.getUrlName()+"%");
					list.add(p);
				}
				if (permListDTO != null && !StringUtils.isEmpty(permListDTO.getParentTree())) {
					Predicate p = cb.like(root.get("parentTree").as(String.class), "%"+permListDTO.getParentTree()+"%");
					list.add(p);
				}
				Predicate[] predicates = new Predicate[list.size()];
				return cb.and(list.toArray(predicates));
			}
			
		};
		return spec;
	}
}
